package org.liaohailong.library.widget.interact

import android.content.Context
import android.graphics.Canvas
import android.graphics.Path
import android.util.AttributeSet
import android.widget.FrameLayout
import org.liaohailong.library.R

/**
 * Author: liaohailong
 * Date: 2019/3/12
 * Time: 9:14 AM
 * Description: 可自定义圆角的FrameLayout
 **/
class RoundRectFrameLayout(context: Context, attributeSet: AttributeSet?, defStyle: Int) : FrameLayout(context, attributeSet, defStyle) {
    constructor(context: Context, attributeSet: AttributeSet?) : this(context, attributeSet, 0)
    constructor(context: Context) : this(context, null)

    private val contentPath: Path = Path()

    private var leftTopRadiusPx: Int = 0
    private var leftBottomRadiusPx: Int = 0
    private var rightTopRadiusPx: Int = 0
    private var rightBottomRadiusPx: Int = 0

    init {
        attributeSet?.let {
            val array = context.obtainStyledAttributes(it, R.styleable.RoundRectFrameLayout)
            leftTopRadiusPx = array.getDimensionPixelSize(R.styleable.RoundRectFrameLayout_leftTopRadius, 0)
            leftBottomRadiusPx = array.getDimensionPixelSize(R.styleable.RoundRectFrameLayout_leftBottomRadius, 0)
            rightTopRadiusPx = array.getDimensionPixelSize(R.styleable.RoundRectFrameLayout_rightTopRadius, 0)
            rightBottomRadiusPx = array.getDimensionPixelSize(R.styleable.RoundRectFrameLayout_rightBottomRadius, 0)
            array.recycle()
        }
    }

    override fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) {
        super.onSizeChanged(w, h, oldw, oldh)
        contentPath.reset()
        //内容区域
        contentPath.addRect(0F, 0F, w.toFloat(), h.toFloat(), Path.Direction.CW)
        //左上圆
        val leftTopCirclePath = Path()
        leftTopCirclePath.addCircle(
                leftTopRadiusPx.toFloat(),
                leftTopRadiusPx.toFloat(),
                leftTopRadiusPx.toFloat(),
                Path.Direction.CW)
        //左上矩形
        val leftTopRectPath = Path()
        leftTopRectPath.addRect(
                0F,
                0F,
                leftTopRadiusPx.toFloat(),
                leftTopRadiusPx.toFloat(),
                Path.Direction.CW)
        //左下圆
        val leftBottomCirclePath = Path()
        leftBottomCirclePath.addCircle(
                leftBottomRadiusPx.toFloat(),
                h - leftBottomRadiusPx.toFloat(),
                leftBottomRadiusPx.toFloat(),
                Path.Direction.CW)
        //左下矩形
        val leftBottomRectPath = Path()
        leftBottomRectPath.addRect(
                0F,
                h - leftBottomRadiusPx.toFloat(),
                leftBottomRadiusPx.toFloat(),
                h.toFloat(),
                Path.Direction.CW)
        //右上圆
        val rightTopCirclePath = Path()
        rightTopCirclePath.addCircle(
                w - rightTopRadiusPx.toFloat(),
                rightTopRadiusPx.toFloat(),
                rightTopRadiusPx.toFloat(),
                Path.Direction.CW)
        //右上矩形
        val rightTopRectPath = Path()
        rightTopRectPath.addRect(
                w - rightTopRadiusPx.toFloat(),
                0F,
                w.toFloat(),
                rightTopRadiusPx.toFloat(),
                Path.Direction.CW)
        //右下圆
        val rightBottomCirclePath = Path()
        rightBottomCirclePath.addCircle(
                w - rightBottomRadiusPx.toFloat(),
                h - rightBottomRadiusPx.toFloat(),
                rightBottomRadiusPx.toFloat(),
                Path.Direction.CW)
        //右下矩形
        val rightBottomRectPath = Path()
        rightBottomRectPath.addRect(
                w - rightBottomRadiusPx.toFloat(),
                h - rightBottomRadiusPx.toFloat(),
                w.toFloat(),
                h.toFloat(),
                Path.Direction.CW)
        //开始裁剪
        contentPath.op(leftTopRectPath, Path.Op.DIFFERENCE)
        contentPath.op(leftBottomRectPath, Path.Op.DIFFERENCE)
        contentPath.op(rightTopRectPath, Path.Op.DIFFERENCE)
        contentPath.op(rightBottomRectPath, Path.Op.DIFFERENCE)
        //开始补半圆
        contentPath.op(leftTopCirclePath, Path.Op.UNION)
        contentPath.op(leftBottomCirclePath, Path.Op.UNION)
        contentPath.op(rightTopCirclePath, Path.Op.UNION)
        contentPath.op(rightBottomCirclePath, Path.Op.UNION)
    }

    override fun draw(canvas: Canvas?) {
        if (canvas == null) return
        val save = canvas.save()
        canvas.clipPath(contentPath)
        super.draw(canvas)
        canvas.restoreToCount(save)
    }

    override fun dispatchDraw(canvas: Canvas?) {
        if (canvas == null) return
        val save = canvas.save()
        canvas.clipPath(contentPath)
        super.dispatchDraw(canvas)
        canvas.restoreToCount(save)
    }
}